from django.db import models

from company.models import Vehicle, VehicleCompanyTerminal
from terminal.models import Terminal


class Media(models.Model):
    """多媒体数据

    接收`多媒体事件信息上传: 0x0800`的信息并存储
    """
    CategoryChoice = (
        (0, '图像'),
        (1, '音频'),
        (2, '视频'),
    )
    EncodingChoice = (
        (0, 'JPEG'),
        (1, 'TIF'),
        (2, 'MP3'),
        (3, 'WAV'),
        (4, 'WMV'),
        (5, 'MP4')
    )
    EventCodeChoice = (
        (0, '平台下发指令'),
        (1, '定时动作'),
        (2, '抢劫报警触发'),
        (3, '碰撞侧翻报警触发'),
        (4, '门开拍照'),
        (5, '门关拍照'),
        (6, '车门由开变关，时速从＜20 公里到超过 20 公里'),
        (7, '定距拍照'),
        (8, '其它')
    )

    DelFlag = (
        (0, '保留'),
        (1, '删除'),
    )

    # 808
    terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE, db_index=True, verbose_name='终端')
    category = models.SmallIntegerField(choices=CategoryChoice, default=CategoryChoice[0][0], verbose_name='多媒体类型')
    encoding_type = models.SmallIntegerField(choices=EncodingChoice, default=EncodingChoice[0][0], verbose_name='编码类型')
    event_code = models.SmallIntegerField(choices=EventCodeChoice, default=EventCodeChoice[0][0], verbose_name='事件项编码')
    channel_id = models.PositiveIntegerField(default=0, verbose_name='通道ID')
    start_time = models.DateTimeField(verbose_name='开始时间')
    end_time = models.DateTimeField(verbose_name='结束时间')
    del_flag = models.SmallIntegerField(choices=DelFlag, default=EventCodeChoice[0][0], verbose_name='删除标志')

    # 自定义
    uri = models.CharField(max_length=200, verbose_name='多媒体信息存储URI地址')
    created_time = models.DateTimeField(auto_now_add=True, editable=False)

    class Meta:
        db_table = 'media'

    @property
    def video_type(self):  # 获取视频或者路况类型
        if self.uri[0] == "A":
            return '司机视频'
        return '路况视频'

    @property
    def license_number(self):  # 获取车牌
        vct = VehicleCompanyTerminal.objects.filter(terminal=self.terminal).first()
        return vct.vehicle.license_number


class VideoStream(models.Model):
    """
    """
    URICategoryChoice = (
        (1, 'RTMP视频源'),
        (2, 'RTSP视频源'),
        (3, 'HTTP视频源'),
    )

    ActionChoice = (
        (0, '播放'),
        (1, '停止播放')
    )

    terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE, db_index=True, verbose_name='终端')
    channel_id = models.SmallIntegerField(default=1, verbose_name='通道ID')
    uri = models.CharField(max_length=200, verbose_name='多媒体信息存储URI地址')
    action = models.SmallIntegerField(choices=ActionChoice, default=ActionChoice[0][0], verbose_name='播放/停止播放')
    uri_category = models.SmallIntegerField(choices=URICategoryChoice, default=URICategoryChoice[0][0],
                                            verbose_name='URI资源类型')
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'video_stream'
